/**
 * Desktop Like Interface WindowContainer
 * copyrigth © 2006-2007 geolabs
 * author : Gérald Fenoy gerald [ at ] geolabs [ dot ] fr
 *
 */
System.require("Window");
System.require("Position");

Desktop=Class.create();
Desktop.define({

  _init: function(name){
      this.name=name;
      this.size=Position.fullSize();
      this.size={x: this.size.x-20,y: this.size.y-15}
      if(!System.document.getElementById(name+"bg")){
	this.bg = System.document.createElement('div');
	this.bg.setAttribute('id', name+'bg');
	if(!Styler._unbound)
	  this.bg.setAttribute('style', "clear: none; position: absolute;top: 5px;left: 5px;z-index: 25;display: none;");
	else{
	  this.bg.style.clear='none';
	  this.bg.style.position='absolute';
	  this.bg.style.display='none';
	  this.bg.style.top='5px';
	  this.bg.style.left='5px';
	  //this.bg.style.width=(this.size.x)+'px';
	  //this.bg.style.height=(this.size.y)+'px';
	  Styler.setOpacity(this.bg,0.1);
	  this.bg.style.backgroundColor='#fff';
	  this.bg.style.zIndex=25;
	  this.bg.onmouseover=new Function();
	  this.bg.appendChild(System.document.createTextNode(" "));
	}
	this.bg.onclik=this.hideBg.bind(this);
	System.document.body.appendChild(this.bg);
      }
      else{
	this.bg=System.document.getElementById(name+"bg");
      }
      this.resize();
      /*
      if(!System.document.getElementById(name)){
	this.form = System.document.createElement('form');
	this.form.setAttribute('id', name);
	this.form.setAttribute('name', name);
	var tmp1=System.document.createElement('input');
	tmp1.setAttribute('type','hidden');
	tmp1.setAttribute('name','activeWindow');
	tmp1.setAttribute('id','activeWindow');
	this.form.appendChild(tmp1);
	var tmp2=System.document.createElement('input');
	tmp2.setAttribute('type','hidden');
	tmp2.setAttribute('name','orderedWindow');
	tmp2.setAttribute('id','orderedWindow');
	this.form.appendChild(tmp2);
	System.document.body.appendChild(this.form);
      }
      else{
	this.form=System.document.getElementById(name);
      }
      */
      this.allwin=new Array();
      this.activeZIndex=190;

    },

      /*updateDesktop: function(){
      for(i=0;i<this.allwin.length;i++)
	if(this.allwin[i].name!=this.form.activeWindow.value)
	  this.allwin[i].unsetFocus();
	  },*/

  displayBg: function(){
      this.bg.style.display='block';
    },

  hideBg: function(){
      this.bg.style.display='none';
    },

  resize: function(){
      this.size=Position.fullSize();
      this.size={x: this.size.x-20,y: this.size.y-15}
      this.bg.style.width=(this.size.x)+'px';
      this.bg.style.height=(this.size.y)+'px';
    },

  dumpPos: function(){
      var tmp=new Array();
      for(i=0;i<this.allwin.length;i++)
	tmp.push(new Array(this.allwin[i].name, this.allwin[i].getPosition()));
      alert(tmp);
    },

  getOrderedWindows: function(){
      if(System.debug)
	alert(tmp+' \n+ nb éléments : '+ret.length+' \n+ Premier élément : '+ret[0]+' \n+ Dernier éléments : '+ret[ret.length-2]);
      return this.allwin;
    },
 
  setActiveWindow: function(window){
      //this.form.activeWindow.value=''+name+'';
      var newAllwin=new Array();
      var j=0;
      ifound=-1;
      for(i=0;i<this.allwin.length;i++){
	if(this.allwin[i].name!=window.name){
	  newAllwin.push(this.allwin[i]);
	  j++;
	}
      }
      newAllwin.push(window);
      this.allwin=newAllwin;
      this.updateWindowsZIndex();
    },
 
  updateWindowsZIndex: function(){
      var toRemove=new Array();
      for(i=this.allwin.length-1;i>=0;i--){
	if(System.document.getElementById(this.allwin[i].name))
	  System.document.getElementById(this.allwin[i].name).style.zIndex=130+(i*2);
	else
	  toRemove.push(this.allwin[i].name);
      }
      for(var i=0;i<toRemove.length;i++)
	this.removeWin(toRemove[i]);
    },

  removeWin: function(win){
      var tmp=new Array();
      var exists=false;
      for(i=0;i<this.allwin.length;i++)
	if(this.allwin[i].name!=win.name){
	  tmp.push(this.allwin[i]);
	}else{exists=i;}
      var tmp1=this.allwin[i];
      this.allwin=tmp;
      delete(tmp1);
    },

  add: function(win){

      win.setId(this.allwin.length);
      win.setDesktop(this);
      this.setActiveWindow(win);
      //this.form.orderedWindow.value+=''+win.name+',';
      
      win.getOldPosition(System.document.getElementById(win.name));
      
    }

  });
